<?php

/**
 * This is the model class for table "tbl_taikhoan".
 *
 * The followings are the available columns in table 'tbl_taikhoan':
 * @property integer $Id
 * @property string $TaiKhoan
 * @property string $MatKhau
 * @property string $MatKhau2
 * @property string $Salt
 * @property string $Email
 * @property integer $TrangThai
 */
class TaiKhoan extends CActiveRecord
{
	public $MatKhau2;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return TaiKhoan the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_taikhoan';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('TaiKhoan, MatKhau, TrangThai', 'required'),
			array('TaiKhoan', 'unique'),
			array('TrangThai', 'numerical', 'integerOnly'=>true),
			array('TaiKhoan, MatKhau, Salt', 'length', 'max'=>50),
			array('Email', 'length', 'max'=>100),
			array('Email', 'email'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('Id, TaiKhoan, MatKhau, Salt, Email, TrangThai', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'Id' => 'ID',
			'TaiKhoan' => 'Tài khoản',
			'MatKhau' => 'Mật khẩu',
			'Salt' => 'Salt',
			'Email' => 'Email',
			'TrangThai' => 'Trạng thái',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('Id',$this->Id);
		$criteria->compare('TaiKhoan',$this->TaiKhoan,true);
		$criteria->compare('MatKhau',$this->MatKhau,true);
		$criteria->compare('Salt',$this->Salt,true);
		$criteria->compare('Email',$this->Email,true);
		$criteria->compare('TrangThai',$this->TrangThai);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function validatePassword($password)
	{
		return $this->hashPassword($password,$this->Salt)===$this->MatKhau;
	}
	public function validateUsername($username)
	{
	    
	}
	/**
	 * Generates the password hash.
	 * @param string password
	 * @param string salt
	 * @return string hash
	 */
	public function hashPassword($password,$salt)
	{
		return md5($salt.$password);
	}

	/**
	 * Generates a salt that can be used to generate a password hash.
	 * @return string the salt
	 */
	public function generateSalt()
	{
		return uniqid('',true);
	}
	
	public function get_id() {
		if (isset(Yii::app()->user->name)) {
			$sql = "SELECT Id FROM tbl_taikhoan WHERE TaiKhoan = '".Yii::app()->user->name."'";
			$data = $this->findBySql($sql);
			return (isset($data)) ? $data->Id : false;
		} else 
			return false;
	}
}